/**
 * @公司:公约树
 * @位置:苏州
 * @Classname Anime04playToAndAnimeInit
 * @Date 2022-06-24 下午 10:56
 * @Created by hanWanLu
 * @auther 中文:韩万路
 */


package com.animeJavaExample;

import com.animeJava.core.AnimeJava;
import com.animeJava.core.EasingData;
import com.animeJava.dao.animeEvent.AnimeRender;
import com.animeJava.mode.PositionVarModel;
import com.animeJava.mode.animeModel.AnimeHoverProperty;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
 * 动画播放到什么地方与初始化动画的地方
 */
public class Anime07playToQBounce extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    List<AnimeJava> animeJavas = new ArrayList<>();
    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setWidth(500);
        primaryStage.setHeight(500);

        AnchorPane root = new AnchorPane();
        primaryStage.setScene(new Scene(root));



//        AnimeProperty animeProperty = new AnimeProperty();
//        animeProperty.addKeyFrames(0,0D);
//        animeProperty.addKeyFrames(100,1D);
//        animeProperty.setDuration(100);


        for (int i = 0; i < 10; i++) {

            Button box = new Button("");
            root.getChildren().add(box);
            box.setPrefWidth(120);
            box.setPrefHeight(20);
            box.setLayoutX(40);
            box.setLayoutY(40 + (i * 40));

            AnimeHoverProperty animeProperty = new AnimeHoverProperty();
            animeProperty.setPlayOneKey(true);
            animeProperty.setDuration(800);
            animeProperty.inPosition(40D);
            animeProperty.setEasing(new EasingData(0.34,1.55,0.40,0.88));
            AnimeJava animeJava = new AnimeJava(animeProperty);
            animeJavas.add(animeJava);
            animeJava.update(new AnimeRender() {
                @Override
                public boolean run(Double timeTo, Map<String, PositionVarModel> animeInfo, List<Double> data) {
                    if(data.get(0) != null){
                        Platform.runLater(()->{
                            box.setLayoutX(data.get(0));
                        });
                    }
                    return true;
                }
            });
        }



        Button toggle = new Button("切换滑动位置");
        root.getChildren().add(toggle);

        final int[] is ={0};
        toggle.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("切换-------------------");

                if(is[0] == 0){
                    new Thread(()->{
                        for (int i = 0; i < animeJavas.size(); i++) {
                            int finalI = i;
                            Platform.runLater(()->animeJavas.get(finalI).playTo(300D));
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                        }
                    }).start();

                }else if(is[0] == 1){
                    new Thread(()->{
                        for (int i = 0; i < animeJavas.size(); i++) {
                            int finalI = i;
                            Platform.runLater(()->animeJavas.get(finalI).playTo(40D));
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }

                        }
                    }).start();

                }
                is[0]++;
                if(is[0] >= 2){
                    is[0] = 0;
                }
            }
        });
        primaryStage.show();
    }
}
